/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author kpatel
 */
/*
 * This will model RAM for the architecture.
 * This class will create a RAM and load data from a text file to RAM.
 *
 *
 */
//import java.lang.reflect.InvocationTargetException;
//import javax.swing.JOptionPane;
import java.io.*;
import javax.swing.JOptionPane;


public class Ram
{

    public static int input_data_start = 0,input_data_end = 0;
    public static int input_coefficient_start = 0,input_coefficient_end = 0;
    public static int[] ram;
    public static File in_data_file;
    String data_line = null;
	// This is constructor of class Ram. We will fill RAM by reading a text.

    public void Load_Coefficients()
    {

        try
		{
			BufferedReader in_data_buffer = new BufferedReader(new FileReader(in_data_file));

			int data_line_int;
			int ram_array_index=0;

			while((data_line = in_data_buffer.readLine()) != null)
			{
				//System.out.println("The input string is " + data_line);
                input_coefficient_start = data_line.compareTo("filter_coefficients_start");

                if(input_coefficient_start==1 && input_coefficient_end==0)
                    {
                    
                    input_coefficient_end = data_line.compareTo("filter_coefficients_end");
                    data_line_int = Integer.parseInt(data_line);
                    System.out.println(data_line_int);
                    ram[ram_array_index] = data_line_int;
                    ram_array_index ++;
                    }

			}
            //JOptionPane.showMessageDialog(null,"The coefficients are loaded");
//			for(int i=0;i<ram_array_index;i++)
//			{
//				//System.out.print("i="+i+"\n" );
//				//System.out.print("The input to ram is " + ram[i]+ "\n");
//				System.out.print("ram ["+i+"] = " + ram[i]+ "\n");
//
//			}


		}catch (FileNotFoundException e)
			{
				e.printStackTrace();
				System.out.println("The input file for RAM is not found!");

			}catch (IOException e)
			{
				e.printStackTrace();

            }
    }

        public void Load_Data()
    {

        try
		{
			BufferedReader in_data_buffer = new BufferedReader(new FileReader(in_data_file));

			int data_line_int;
			int ram_array_index=0;

			while((data_line = in_data_buffer.readLine()) != null)
			{
				System.out.println("The input string is " + data_line);
                input_data_start = data_line.compareTo("input_data_start");

                if(input_data_start==1 && input_data_end==0)
                    {

                    input_data_end = data_line.compareTo("input_data_end");
                    data_line_int = Integer.parseInt(data_line);
                    System.out.println(data_line_int);
                    ram[ram_array_index] = data_line_int;
                    ram_array_index ++;
                    }
			}
            //JOptionPane.showMessageDialog(null,"The data is loaded");
//			for(int i=0;i<ram_array_index;i++)
//			{
//				//System.out.print("i="+i+"\n" );
//				//System.out.print("The input to ram is " + ram[i]+ "\n");
//				System.out.print("ram ["+i+"] = " + ram[i]+ "\n");
//
//			}

		}catch (FileNotFoundException e)
			{
				e.printStackTrace();
				System.out.println("The input file for RAM is not found!");

			}catch (IOException e)
			{
				e.printStackTrace();

            }
    }

	public Ram(int ram_size)
    {

		in_data_file = new File ("C:/Users/kpatel/workspace/ISS_SIMD_FIR_16points_256samples/src/test.txt");
		//StringBuffer contents = new StringBuffer();
		data_line = null;
        ram = new int [ram_size];
        
    }

}